﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wechat.Dao;
using Wechat.Util.Paged;

namespace Wechat.Manager.Implement
{
    public abstract class GenericManagerBase<T> : IGenericManager<T> where T : class
    {
        public IRepository<T> CurrentRepository { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="repository"></param>
        public virtual T Get(int id)
        {
            if (id <= 0)
            {
                return null;
            }
            return CurrentRepository.Get(id);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="table">根据字段</param>
        /// <param name="key">根据字段</param>
        /// <param name="value">字段值</param>
        /// <returns></returns>
        public virtual IList<T> GetByKey(string table, string key, object value)
        {
            return CurrentRepository.GetByKey(table, key, value);
        }

        /// <summary>
        /// 获取全部实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual IList<T> GetAll()
        {
            return CurrentRepository.GetAll();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual int Insert(T entity)
        {
            return CurrentRepository.Insert(entity);
        }

        /// <summary>
        /// 批量插入实体
        /// </summary>
        /// <param name="entityList"></param>
        /// <returns></returns>
        public int Insert(IEnumerable<T> entityList)
        {
            return CurrentRepository.Insert(entityList);
        }

        /// <summary>
        /// 批量插入，事务处理
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int InsertTran(IEnumerable<T> list)
        {
            return CurrentRepository.InsertTran(list);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            return CurrentRepository.Update(entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            return CurrentRepository.Delete(entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual IList<T> LoadByCondition(string query, object param)
        {
            return CurrentRepository.LoadByCondition(query, param);
        }

        /// <summary>
        /// 根据条件分页查询
        /// </summary>
        /// <param name="tablename">表名,多表是请使用 tA a inner join tB b On a.AID = b.AID </param>
        /// <param name="key">主键，可以带表头 a.AID </param>
        /// <param name="PageNum">页编码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="totalRecordCount">记录总条数</param>
        /// <param name="fields">查询字段</param>
        /// <param name="condition">WHERE条件</param>
        /// <param name="sort">排序字段</param>
        /// <returns></returns>
        public virtual PagedList<TModel> GetPageDataList<TModel>(PageCriteria criteria) where TModel : class
        {
            return CurrentRepository.GetPageDataList<TModel>(criteria);
        }


        /// <summary>
        /// 执行简单存储过程，返回受影响行数
        /// </summary>
        /// <param name="param"></param>
        /// <param name="procName"></param>
        /// <returns></returns>
        public virtual int ExecByProcedure(object param, string procName)
        {
            return CurrentRepository.ExecByProcedure(param, procName);
        }

        /// <summary>
        /// 执行sql，返回受影响的行数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="param">参数对象</param>
        /// <returns></returns>
        public virtual int ExecuteSql(string sql, object param)
        {
            return CurrentRepository.ExecuteSql(sql, param);
        }


        /// <summary>
        /// 执行sql，返回一行一列值
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="param">参数对象</param>
        /// <returns></returns>
        public virtual object ExecuteScalar(string sql, object param = null)
        {
            return CurrentRepository.ExecuteScalar(sql, param);
        }
    }
}
